package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCustomProcessBaseGoodsRel
//@description: 创建CustomProcessBaseGoodsRel记录
//@param: customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel
//@return: err error

func CreateCustomProcessBaseGoodsRel(customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel) (err error) {
	err = global.GVA_DB.Create(&customProcessBaseGoodsRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseGoodsRel
//@description: 删除CustomProcessBaseGoodsRel记录
//@param: customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel
//@return: err error

func DeleteCustomProcessBaseGoodsRel(customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel) (err error) {
	err = global.GVA_DB.Delete(customProcessBaseGoodsRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseGoodsRelByIds
//@description: 批量删除CustomProcessBaseGoodsRel记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCustomProcessBaseGoodsRelByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CustomProcessBaseGoodsRel{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCustomProcessBaseGoodsRel
//@description: 更新CustomProcessBaseGoodsRel记录
//@param: customProcessBaseGoodsRel *model.CustomProcessBaseGoodsRel
//@return: err error

func UpdateCustomProcessBaseGoodsRel(customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel) (err error) {
	err = global.GVA_DB.Save(&customProcessBaseGoodsRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseGoodsRel
//@description: 根据id获取CustomProcessBaseGoodsRel记录
//@param: id uint
//@return: err error, customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel

func GetCustomProcessBaseGoodsRel(id uint) (err error, customProcessBaseGoodsRel model.CustomProcessBaseGoodsRel) {
	err = global.GVA_DB.Where("id = ?", id).First(&customProcessBaseGoodsRel).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseGoodsRelInfoList
//@description: 分页获取CustomProcessBaseGoodsRel记录
//@param: info request.CustomProcessBaseGoodsRelSearch
//@return: err error, list interface{}, total int64

func GetCustomProcessBaseGoodsRelInfoList(info request.CustomProcessBaseGoodsRelSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CustomProcessBaseGoodsRel{})
    var customProcessBaseGoodsRels []model.CustomProcessBaseGoodsRel
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.BaseId != 0 {
        db = db.Where("`base_id` = ?",info.BaseId)
    }
    if info.GoodsId != 0 {
        db = db.Where("`goods_id` = ?",info.GoodsId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&customProcessBaseGoodsRels).Error
	return err, customProcessBaseGoodsRels, total
}